package com.lyeducation.controllers.category;

import com.lyeducation.entities.course.CategoryEntity;
import com.lyeducation.services.category.CategoryService;
import com.lyeducation.util.StringUtils;
import com.lyeducation.util.controllerUtil.JsonConstans;
import com.lyeducation.util.controllerUtil.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName CategoryController
 * @Description
 * @Author Sky1in3
 * @Date 2019/1/26 16:29
 * @Version 1.0
 */
@Api(value = "类目", tags = {"类目模块接口，{mId}菜单id"}, description = "类目模块接口")
@RestController
@RequestMapping("/category/{mid}")
@Transactional(rollbackFor = Exception.class, readOnly = false, propagation = Propagation.REQUIRED)
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @ApiOperation(value = "新增类目", notes = "新增类目")
    @PostMapping("/newCategory")
    public JsonResult newCategory(@RequestBody CategoryEntity categoryEntity) {
        if (StringUtils.isEmpty(categoryEntity.getId())) {
            return JsonResult.putSuccess(categoryService.addCategory(categoryEntity));
        }
        return JsonResult.putFail(JsonConstans.OPERATION_FAILURE);
    }

    @ApiOperation(value = "修改类目", notes = "修改类目")
    @PostMapping("/updateCategory")
    public JsonResult updateCategory(@RequestBody CategoryEntity categoryEntity) {
        if (StringUtils.isNotBlank(categoryEntity.getId())) {
            return JsonResult.putSuccess(categoryService.updateCategory(categoryEntity));
        }
        return JsonResult.putFail(JsonConstans.OPERATION_FAILURE);
    }

    @ApiOperation(value = "删除类目", notes = "删除类目")
    @GetMapping("/deleteCategory/{categoryId}")
    public JsonResult deleteCategory(@PathVariable String categoryId) {
        if (StringUtils.isNotBlank(categoryId)) {
            return JsonResult.putSuccess(categoryService.deleteCategory(categoryId));
        }
        return JsonResult.putFail(JsonConstans.OPERATION_FAILURE);
    }

    @ApiOperation(value = "类目详情", notes = "类目详情")
    @GetMapping("/categoryInfo/{categoryId}")
    public JsonResult categoryInfo(@PathVariable String categoryId) {
        if (StringUtils.isNotBlank(categoryId)) {
            return JsonResult.putSuccess(categoryService.findOne(categoryId));
        }
        return JsonResult.putFail(JsonConstans.OPERATION_FAILURE);
    }

    @ApiOperation(value = "类目列表", notes = "类目列表")
    @GetMapping("/categoryList/{pageNo}/{pageSize}")
    public JsonResult categoryList(@PathVariable("pageNo") int pageNo, @PathVariable("pageSize") int pageSize) {
        return JsonResult.putSuccess(categoryService.categoryEntityList(pageNo, pageSize));
    }
}
